package com.cropdoctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cropdoctor.common.CustomException;
import com.cropdoctor.common.Result;
import com.cropdoctor.mapper.NoteMapper;
import com.cropdoctor.model.dto.NoteDto;
import com.cropdoctor.model.po.NotePo;
import com.cropdoctor.service.NoteService;
import com.cropdoctor.util.AliOssUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    AliOssUtil ossUtil;

    @Autowired
    private NoteMapper noteMapper;

    @Override
    public Result<String> insertImgById(int id, MultipartFile file) {
        String url = null;
        try {
            //            获取原始的文件名
            String originalFilename = file.getOriginalFilename();
            //在oss中存储名字就是UUID + 文件的后缀名
            String objectName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            url = ossUtil.upload(file.getBytes(), "remark/"+objectName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("上传图片失败!");
        }
        LambdaUpdateWrapper<NotePo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NotePo::getId,id).set(NotePo::getUrl,url);
        noteMapper.update(null,wrapper);

        return Result.success("上传图片成功!");
    }

    @Override
    public Result<String> insertOrUpdateNoteWithoutImg(NotePo notePo) {
        int result = 0;
        if(notePo.getId() != null && notePo.getId() != 0){
            result = noteMapper.updateById(notePo);
            return result > 0 ? Result.success("更新笔记成功!"):Result.error("更新笔记失败!");
        }else {
            result = noteMapper.insert(notePo);
            return result > 0 ? Result.success("新增笔记成功!") : Result.error("新增笔记失败!");
        }
    }


    @Override
    public Result<List<NotePo>> getNoteById(int idGarden) {
        LambdaQueryWrapper<NotePo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(NotePo::getIdGarden, idGarden);
        List<NotePo> notePos = noteMapper.selectList(lambdaQueryWrapper);
        return Result.success(notePos);
    }

    @Override
    public Result<String> updateNoteById(NotePo notePo) {
        int result = noteMapper.updateById(notePo);
        return result > 0 ? Result.success("更新备注信息成功!"):Result.error("更新备注信息失败!");
    }

    @Override
    public Result<String> insertOrUpdateNote(NotePo notePo, MultipartFile file) {
        String url = null;
        if(file != null){
            try {
                //            获取原始的文件名
                String originalFilename = file.getOriginalFilename();
                //在oss中存储名字就是UUID + 文件的后缀名
                String objectName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                url = ossUtil.upload(file.getBytes(), "note/"+objectName);
            } catch (IOException e) {
                e.printStackTrace();
                throw new CustomException("保存图片失败!");
            }
            notePo.setUrl(url);
        }
        int result;
        if(notePo.getId() != null){
            result = noteMapper.updateById(notePo);
            return result > 0 ? Result.success("更新笔记成功!"):Result.error("更新笔记失败!");
        }
        result = noteMapper.insert(notePo);
        return result > 0 ? Result.success("新增笔记成功!"):Result.error("新增笔记失败!");
    }

    @Override
    public Result<String> deleteNote(int noteId) {
        int result = noteMapper.deleteById(noteId);
        return result > 0 ? Result.success("删除备注信息成功!"):Result.error("删除备注信息失败!");
    }


}
